// pages/orderList/orderList.js
const app = getApp()
import {
  orderList,
  rePayOrder
} from '../../api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    nav: [{
        name: '全部',
        id: -1
      },
      {
        name: '待付款',
        id: 1
      },
      {
        name: '待收货',
        id: 3
      },
      {
        name: '已完成',
        id: 5
      },
    ],
    list: [],
    current: 0,
    status: -1,
    //顶部提示
    topType: 'success',
    topInfo: '',
    showTop: false
  },
  choose(e) {
    console.log(e);
    this.setData({
      current: e.currentTarget.dataset.index,
      page: 1,
      status: e.currentTarget.dataset.id,
      list: []
    })
    let obj = {
      token: app.globalData.userInfo.token,
      page: this.data.page
    }
    if (this.data.status != -1) {
      obj.status = this.data.status
    }
    this.getOrder(obj)
  },
  //重新支付
  rePay(e) {
    console.log(e);
    rePayOrder({
      token: app.globalData.userInfo.token,
      order_id: e.currentTarget.dataset.index
    }).then(res => {
      console.log(res);
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: String(res.data.nonce_str),
        package: `prepay_id=${res.data.prepay_id}`,
        signType: 'MD5',
        paySign: String(res.data.paySign),
        success:(res)=> {
          this.setData({
            topType: 'info',
            topInfo: '支付成功',
            showTop: true
          })
          let obj = {
            token: app.globalData.userInfo.token,
            page: this.data.page
          }
          // if (this.data.status != -1) {
          //   obj.status = this.data.status
          // }
          this.getOrder(obj)
        },
        fail(res) {
          console.log(res);
        }
      })
    })
  },
  //获取订单
  getOrder(obj) {
    orderList(obj).then(res => {
      if (res.data.length <= 0) {
        return
      }
      res.data.data.forEach(item => {
        item.childern.forEach(it => {
          it.sku = JSON.parse(it.sku)
        })
        if (item.status == 1) {
          item.status_text = '待支付'
        }
        if (item.status == 2) {
          item.status_text = '已支付'
        }
        if (item.status == 3) {
          item.status_text = '未发货'
        }
        if (item.status == 4) {
          item.status_text = '已发货'
        }
        if (item.status == 5) {
          item.status_text = '已收货'
        }
        if (item.status == 6) {
          item.status_text = '已退款'
        }
        if (item.status == 7) {
          item.status_text = '已取消'
        }
      })
      this.setData({
        list: res.data.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      let index = this.data.nav.findIndex(item => item.id == options.id)
      this.setData({
        status: options.id,
        current: index
      })
    }
    let obj = {
      token: app.globalData.userInfo.token,
      page: this.data.page
    }
    if (this.data.status != -1) {
      obj.status = this.data.status
    }
    this.getOrder(obj)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})